package com.cloud.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloud.common.R;
import com.cloud.excption.BizException;
import com.cloud.mall.user.TbMember;
import com.cloud.user.service.ILoginService;
import com.cloud.user.service.ITbMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户表 服务类
 * </p>
 *
 * @author jobob
 * @since 2021-07-07
 */
@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    ITbMemberService iTbMemberService;
    @Autowired
    StringRedisTemplate redisTemplate;


    public R sendPhoneCode(String phone) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", phone);
        List<TbMember> list = iTbMemberService.list(queryWrapper);
        if (list == null || list.size() == 0) {
            throw new BizException("500", "手机号码不存在");
        }
        String code = redisTemplate.opsForValue().get(phone);
        if (null == code) {
//            可以继续生成验证码
            redisTemplate.opsForValue().set(phone, "111111", 60, TimeUnit.SECONDS);
            return new R.Builder().buildOk();
        } else {
            return new R.Builder().buildCustomize(500, "验证码未到时");
        }
    }

}
